package cn.iocoder.yudao.module.measure.service.point.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.measure.controller.admin.point.VO.MeasurePointInfoPageReqVO;
import cn.iocoder.yudao.module.measure.controller.admin.point.VO.MeasurePointInfoSaveReqVO;
import cn.iocoder.yudao.module.measure.dal.dataobject.point.MeasurePointInfoDO;
import cn.iocoder.yudao.module.measure.dal.dataobject.upload.BusinessDeviceUploadData;
import cn.iocoder.yudao.module.measure.dal.dataobject.upload.MeasureData;
import cn.iocoder.yudao.module.measure.dal.mysql.point.BusinessMeasurePointInfoMapper;
import cn.iocoder.yudao.module.measure.service.point.BusinessMeasurePointInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.measure.enums.ErrorCodeContants.MEASURE_POINT_INFO_NOT_EXISTS;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 业务实现类
 * 测量点信息表
 * </p>
 *
 * @author zhengbzh
 * @date 2025-06-09 16:47:58
 * @create [2025-06-09 16:47:58] [zhengbzh] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BusinessMeasurePointInfoServiceImpl implements BusinessMeasurePointInfoService {


    @Resource
    private BusinessMeasurePointInfoMapper measurePointInfoMapper;

    @Override
    public MeasureData queryMeasureData(MeasureData queryParams, Map<String, String> contentMap) {
        // 根据查询条件去数据库查询
        List<BusinessDeviceUploadData> list = measurePointInfoMapper.queryMeasureData(queryParams);
        // 将查询结果处理成期望的格式
        for (BusinessDeviceUploadData uploadData : list) {
            queryParams.add(uploadData);
        }

        // 将计算过程中的极值转为计算结果 并处理转码
        queryParams.covertToFinish(contentMap);

        return queryParams;
    }

    @Override
    @Transactional
    public Long createMeasurePointInfo(MeasurePointInfoSaveReqVO createReqVO) {
        // 插入
        MeasurePointInfoDO measurePointInfo = BeanUtils.toBean(createReqVO, MeasurePointInfoDO.class);
        measurePointInfoMapper.insert(measurePointInfo);

        // 返回
        return measurePointInfo.getId();
    }

    @Override
    @Transactional
    public void updateMeasurePointInfo(MeasurePointInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateMeasurePointInfoExists(updateReqVO.getId());
        // 更新
        MeasurePointInfoDO updateObj = BeanUtils.toBean(updateReqVO, MeasurePointInfoDO.class);
        measurePointInfoMapper.updateById(updateObj);
    }

    @Override
    @Transactional
    public void deleteMeasurePointInfo(Long id) {
        // 校验存在
        validateMeasurePointInfoExists(id);
        // 删除
        measurePointInfoMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteMeasurePointInfoListByIds(List<Long> ids) {
        // 删除
        measurePointInfoMapper.deleteByIds(ids);
    }


    private void validateMeasurePointInfoExists(Long id) {
        if (measurePointInfoMapper.selectById(id) == null) {
            throw exception(MEASURE_POINT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public MeasurePointInfoDO getMeasurePointInfo(Long id) {
        return measurePointInfoMapper.selectById(id);
    }

    @Override
    public PageResult<MeasurePointInfoDO> getMeasurePointInfoPage(MeasurePointInfoPageReqVO pageReqVO) {
        return measurePointInfoMapper.selectPage(pageReqVO);
    }
}


